package com.ruoyi.web.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.ruoyi.web.entity.Custom;
import com.ruoyi.web.service.ICustomService;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/admin/custom")
public class CustomController extends BaseController {

    private final ICustomService customService;
    public CustomController(ICustomService customServiceImpl){
        this.customService = customServiceImpl;
    }
    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('admin:custom:list')")
    @GetMapping("/list")
    public TableDataInfo list(Custom custom)
    {
        startPage();
        List<Custom> list = customService.selectCustomList(custom);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('admin:custom:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Custom custom)
    {
        List<Custom> list = customService.selectCustomList(custom);
        ExcelUtil<Custom> util = new ExcelUtil<Custom>(Custom.class);
        util.exportExcel(response, list , "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:custom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(customService.selectCustomById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('admin:custom:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Custom custom)
    {
        custom.setId(IdUtil.getSnowflakeNextId());
        return toAjax(customService.insertCustom(custom));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('admin:custom:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Custom custom)
    {
        return toAjax(customService.updateCustom(custom));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('admin:custom:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customService.deleteCustomByIds(ids));
    }
}
